//
//  WDGroMoreThinkingManager.m
//  WDGroMore
//
//  Created by little People on 2023/7/31.
//

#import "WDGroMoreThinkingManager.h"
#import "WDGroMoreHeader.h"
#import "WDGroMoreJosnManager.h"
#import "WDNSLog.h"

@interface WDGroMoreThinkingManager ()

@end

@implementation WDGroMoreThinkingManager

+ (instancetype)manager {
    static WDGroMoreThinkingManager *_manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [[self alloc] init];
    });
    return _manager;
}

- (void)thinkingConfig {
    
    NSString *appId = [WDGroMoreJosnManager thinkingAppidForSDKConfigFile];
    NSString *serverUrl = [WDGroMoreJosnManager thinkingServerUrlForSDKConfigFile];
    
    if (appId.length == 0 || serverUrl.length == 0) {
        NSLog(@"[ThinkingAnalytics] 配置缺失，无法初始化");
        return;
    }
    
    TDConfig *config = [[TDConfig alloc] init];
    config.appid = appId;
    config.serverUrl = serverUrl;
    if([WDNSLog logEnable]){
        config.mode = TDModeDebug;
    }else{
        config.mode = TDModeNormal;
    }
    config.defaultTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT+0800"];
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970]*1000;
    [TDAnalytics calibrateTime: time];
    [TDAnalytics startAnalyticsWithConfig:config];
    
    
    if([WDNSLog logEnable]){
        [TDAnalytics enableLog:TDLoggingLevelDebug];
    }else{
        [TDAnalytics enableLog:TDLoggingLevelNone];
    }
    
    [TDAnalytics enableThirdPartySharing:TDThirdPartyTypeAdjust | TDThirdPartyTypeIronSource];
    
    NSLog(@"[ThinkingAnalytics] 初始化完成：AppId=%@ ServerUrl=%@", appId, serverUrl);
}

- (void)thinkingTrackEventWithKey:(NSString *)key jsonStr:(id)jsonStr {
    if (!key.length) {
        NSLog(@"[ThinkingAnalytics] 事件 key 为空");
        return;
    }
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    if ([jsonStr isKindOfClass:[NSDictionary class]]) {
        [dict addEntriesFromDictionary:jsonStr];
    } else if ([jsonStr isKindOfClass:[NSString class]]) {
        NSDictionary *temp = [WDGroMoreJosnManager dictionaryWithJsonString:jsonStr];
        if (temp) {
            [dict addEntriesFromDictionary:temp];
        }
    }
    
    NSLog(@"[ThinkingAnalytics] Track: %@, Properties: %@", key, dict);
    
    [TDAnalytics track:key properties:dict];
}

- (void)uploadUserSourceOnceWithAttributes:(NSDictionary<NSString *, id> *)attributes {
    if (!attributes || attributes.count == 0) {
        NSLog(@"[ThinkingAnalytics] ❌ 无用户属性可上报");
        return;
    }

    // 安全过滤 nil
    NSMutableDictionary *safeAttributes = [NSMutableDictionary dictionary];
    [attributes enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) {
        if (value != nil && ![value isKindOfClass:[NSNull class]]) {
            safeAttributes[key] = value;
        }
    }];

    if (safeAttributes.count == 0) {
        NSLog(@"[ThinkingAnalytics] ⚠️ 全部属性为空，不上报");
        return;
    }

    [TDAnalytics userSetOnce:safeAttributes];
    NSLog(@"[ThinkingAnalytics] ✅ userSetOnce 上报: %@", safeAttributes);
}

@end
